package com.zzh.myapplication.service;

import android.content.Context;
import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.icarplus.car.app.MyApplication;
import net.icarplus.car.bean.AcctResultInfo;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.tools.JsonUtil;
import net.icarplus.car.tools.S;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 项目名称：icarplus-car-android
 * 类描述：网络请求方法，handler将数据返回
 * 创建人：Gophy
 * 创建时间：16/3/25 17:48
 * 修改人：Gophy
 * 修改时间：16/3/25 17:48
 * 修改备注：
 */
public class PublicRequest {

    public static void doGetRequest(final String url, final Handler handler, final JSONObject json, final int successCode, final int errorCode) {
        OkHttpUtils.get()
                   .url(url)
                   .addParams("input",json.toString())
                   .build()
                   .execute(new StringCallback() {
                       @Override
                       public void onError(Call call, Exception e) {
                           handler.obtainMessage(errorCode, e.toString()).sendToTarget();
                       }

                       @Override
                       public void onResponse(String response) {
                           if (JsonUtil.isValid(response)) {
                               handler.obtainMessage(successCode, response).sendToTarget();
                           } else {
                               handler.obtainMessage(errorCode, response).sendToTarget();
                           }
                       }
                   });
    }

    public static void doPostRequest(final String url, final Handler handler, final JSONObject json, final int successCode, final int errorCode) {
        OkHttpUtils.post()
                .url(url)
                .addParams("input",json.toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        handler.obtainMessage(errorCode, e.toString()).sendToTarget();
                    }

                    @Override
                    public void onResponse(String response) {
                        if (JsonUtil.isValid(response)) {
                            handler.obtainMessage(successCode, response).sendToTarget();
                        } else {
                            handler.obtainMessage(errorCode, response).sendToTarget();
                        }
                    }
                });
    }

//    public static void doGetRequest() {
//
//    }


    //周边车辆
    public static void queryNearbyCars(Context context, final Handler handler, final int pageNum, final String gearBox, String rentType) {
        JSONObject json = new JSONObject();
        try {
            json.put("city", "");
            json.put("county", "");
            json.put("gearBox", gearBox);
            json.put("pageNum", pageNum);
            //当前经纬度
            json.put("positionLat", "19.947238");
            json.put("positionLon", "110.12358");
            json.put("rentType", rentType);
            json.put("totalNum", "10");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        OkHttpUtils.get()
                .url(UrlValues.NEAYBY_CARS)
                .addParams("input", json.toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        handler.obtainMessage(1000, e.toString()).sendToTarget();
                    }

                    @Override
                    public void onResponse(String response) {
                        if (JsonUtil.isValid(response)) {
//                            MyApplication.acctResultInfo = response;
                            handler.obtainMessage(1000, response).sendToTarget();
                        } else {
                            handler.obtainMessage(3000, response).sendToTarget();
                        }
                    }
                });
    }

    //电子账户客户信息查询（包含钱包信息）
    public static void queryUserInfo(Context context, final Handler handler, final String mobile) {

        JSONObject json = new JSONObject();
        try {
            json.put("mobile", mobile);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.get().url(UrlValues.QUERY_ACCOUNT_INTO_URL).addParams("input", json.toString()).build()
                .execute(new StringCallback() {
                             @Override
                             public void onError(Call call, Exception e) {
                             }

                             @Override
                             public void onResponse(String response) {

                                 if (JsonUtil.isValid(response)) {
                                     String queryList = JsonUtil.getOriginData(response);
                                     if (!S.isEmpty(queryList)) {
                                         Gson gsons = new Gson();
                                         AcctResultInfo acctResultInfo = gsons.fromJson(response,
                                                 new TypeToken<AcctResultInfo>() {
                                                 }.getType());

                                         MyApplication.acctResultInfo = acctResultInfo;

                                     }


                                     handler.obtainMessage(1000, response).sendToTarget();
                                 } else {
                                     handler.obtainMessage(3000, response).sendToTarget();
                                 }
                             }
                         }

                );
    }


    public static void customMainRequest(Context context, final String url, final Handler handler, final JSONObject json) {
        OkHttpUtils.get().url(url)
                .addParams("input", json.toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        handler.obtainMessage(1000, e.toString()).sendToTarget();
                    }

                    @Override
                    public void onResponse(String response) {
                        if (JsonUtil.isValid(response)) {
//                            MyApplication.acctResultInfo = response;
                            handler.obtainMessage(1000, response).sendToTarget();
                        } else {
                            handler.obtainMessage(3000, response).sendToTarget();
                        }
                    }
                });
    }

    public static void customMainRequestPost(Context context, final String url, final Handler handler, final JSONObject json) {
        OkHttpUtils.post().url(url)
                .addParams("input", json.toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        handler.obtainMessage(1000, e.toString()).sendToTarget();
                    }

                    @Override
                    public void onResponse(String response) {
                        if (JsonUtil.isValid(response)) {
//                            MyApplication.acctResultInfo = response;
                            handler.obtainMessage(2000, response).sendToTarget();
                        } else {
                            handler.obtainMessage(3000, response).sendToTarget();
                        }
                    }
                });
    }


}

